# -*- Mode: Python -*- vi:si:et:sw=4:sts=4:ts=4:syntax=python

from pathlib import Path

class Recipe(recipe.Recipe):
    name = 'librsvg'
    version = '2.40.20'
    licenses = [License.LGPLv2Plus]
    deps = ['libcroco', 'gdk-pixbuf', 'pango', 'cairo']
    autoreconf = True
    autoreconf_sh = 'mkdir -p m4 && autoreconf -fiv'
    stype = SourceType.TARBALL
    url = 'gnome://'
    tarball_checksum = 'cff4dd3c3b78bfe99d8fcfad3b8ba1eee3289a0823c0e118d78106be6b84c92b'

    files_libs = ['librsvg-2']
    files_bins = ['rsvg-convert']
    licenses_bins = [License.GPLv2Plus]
    files_loader = ['lib/gdk-pixbuf-2.0/2.10.0/loaders/libpixbufloader-svg%(mext)s']
    files_devel = ['include/librsvg-2.0/librsvg/*.h',
                   'lib/pkgconfig/librsvg-2.0.pc',
                   'lib/librsvg-2.a', 'lib/librsvg-2.la']
    files_typelibs = ['Rsvg-2.0']
    # GTK3 support just builds rsvg-view-3, which is useless for Cerbero
    configure_options = ' --without-gtk3 '

    patches = ['librsvg/option-enable-disable-gtk.patch',
               'librsvg/0001-Use-ACLOCAL_FLAGS.patch',
               'librsvg/0001-build-Fix-enumtypes-on-MinGW-inside-MSYS-Windows.patch']

    def prepare(self):
        if self.config.target_platform in [Platform.DARWIN, Platform.IOS]:
            self.configure_options += ' --disable-Bsymbolic'
        # Fails on cross-ios and cross-win32
        if self.config.cross_compiling():
            self.configure_options += ' --disable-pixbuf-loader'

    async def compile(self):
        # glib-mkenums has #!/usr/bin/env python3, which looks for `python3`,
        # but on Windows Python 3 is `python.exe`. Meson already handles this
        # for us, but Autotools can't.
        # NOTE: Remove this when the recipe is ported to Meson.
        if self.config.platform == Platform.WINDOWS:
            with (Path(self.build_dir) / 'python3').open('w') as f:
                f.write('#!/bin/sh\n"{}" "$@"'.format(self.config.python_exe))
        await super().compile()
